package com.cby.library.tool.crash;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Description:ActivityManager
 *
 * @author 陈宝阳
 * @create 2020/11/17 16:42
 */
public class ActivityStackManager {

    public static final String TAG = "ActivityStackManager";
    private volatile static ActivityStackManager sInstance = null;

    /**
     * Activity堆栈
     */
    private Stack<Activity> mActivityStack = new Stack<>();

    private ActivityStackManager() {

    }

    public static ActivityStackManager getInstance() {
        if (sInstance == null) {
            synchronized (ActivityStackManager.class) {
                if (sInstance == null) {
                    sInstance = new ActivityStackManager();
                }
            }
        }
        return sInstance;
    }

    /**
     * 添加Activity到堆栈中
     */
    public void addActivity(Activity activity) {
        mActivityStack.add(activity);
    }

    /**
     * 移除当前Activity
     */
    public void removeActivity(Activity activity) {
        if (activity != null && mActivityStack.contains(activity)) {
            mActivityStack.remove(activity);
        }
    }

    /**
     * 获取当前Activity(堆栈中最后一个Activity,位于栈顶)
     */
    public Activity getCurrentActivity() {
        if(mActivityStack != null && !mActivityStack.isEmpty()) {
            return mActivityStack.lastElement();
        }
        return null;
    }

    /**
     * 获取指定类名的Activity
     */
    public Activity getActivity(Class<?> cls) {
        if (mActivityStack != null) {
            for (Activity activity : mActivityStack) {
                if (activity.getClass().equals(cls)) {
                    return activity;
                }
            }
        }
        return null;
    }

    /**
     * 结束当前Activity(栈顶)
     */
    public void finishCurrentActivity() {
        Activity activity = mActivityStack.lastElement();
        activity.finish();
    }


    /**
     * 结束当前Activity的前一个Activity
     */
    public void finishPreActivity() {

        if (mActivityStack != null && mActivityStack.size() > 1) {
            Activity preActivity = mActivityStack.get(mActivityStack.size() - 2);
            if (preActivity != null && !preActivity.isFinishing()) {
                preActivity.finish();
            }
        }

    }


    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null && mActivityStack.contains(activity)) {
            mActivityStack.remove(activity);
            activity.finish();
        }
    }

    /**
     * 清理所有的Activity
     */
    public void finishAllActivity() {
        for (Activity activity : mActivityStack) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
        mActivityStack.clear();
    }

    /**
     * 移除自己以外的Activity
     */
    public void removeAboveActivities(Class activityClass) {
        Activity targetActivity = null;
        try {
            while (mActivityStack.size() > 0) {
                Activity activity = mActivityStack.pop();
                if(!activity.getClass().equals(activityClass)) {
                    activity.finish();
                }else {
                    targetActivity = activity;
                }
            }
            if(targetActivity != null) {
                mActivityStack.add(targetActivity);
            }
        } catch (Exception e) {

        }

    }

    public int getAliveActivities() {
        return mActivityStack.size();
    }

    /**
     * 退出应用程序
     */
    public void AppExit() {
        try {
            finishAllActivity();
            // 杀死该应用进程
            android.os.Process.killProcess(android.os.Process.myPid());
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 返回AndroidManifest.xml中注册的所有Activity的class
     *
     * @param context     环境
     * @param packageName 包名
     * @param excludeList 排除class列表
     * @return
     */
    public final static List<Class> getActivitiesClass(Context context, String packageName, List<Class> excludeList) {
        List<Class> returnClassList = new ArrayList<>();
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
            if (packageInfo.activities != null) {
                for (ActivityInfo ai : packageInfo.activities) {
                    Class c;
                    try {
                        c = Class.forName(ai.name);
                        if (Activity.class.isAssignableFrom(c)) {
                            returnClassList.add(c);
                        }
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                if (excludeList != null) {
                    returnClassList.removeAll(excludeList);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnClassList;
    }
}
